package top.canyue.eca_manager.provider;

import top.canyue.eca_manager.bean.Conference;

public class ConferenceProvider {

    public String updateConferenceInfo(Conference conference) {
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE conference SET ");
        if (conference.getName() != null) {
            sql.append("name = #{name},");
        }
        if (conference.getAddress() != null) {
            sql.append("address = #{address},");
        }
        if (conference.getStartTime() != null) {
            sql.append("startTime = #{startTime},");
        }
        if (conference.getEndTime() != null) {
            sql.append("endTime = #{endTime},");
        }
        if (conference.getComm() != null) {
            sql.append("comm = #{comm},");
        }
        if (conference.getPersistent()) {
            sql.append("persistent = #{persistent},");
        }
        sql.append("updateTime= now() ");
        sql.append("WHERE id = #{id} ");
        sql.append("AND isDelete = 0 ");

        return sql.toString();
    }

    public String getUserConferenceByDay(String userId, String date) {
        return "SELECT * FROM conference " +
                "WHERE NOT isDelete AND NOT persistent " +
                "AND TO_DAYS(startTime) = TO_DAYS(#{date}) " +
                "AND id IN (SELECT conference_id FROM conferencedetail WHERE user_id = #{userId}) ";
    }

    public String getUsersHaveConferenceDaysByMonth(String userId, int year, int month) {
        return "SELECT DISTINCT DAY(startTime) FROM conference " +
                "WHERE NOT isDelete AND NOT persistent " +
                "AND YEAR(startTime) = #{year} AND MONTH(startTime) = #{month} " +
                "AND id IN (SELECT conference_id FROM conferencedetail WHERE user_id = #{userId}) ";
    }
}
